/*
 * This file is part of ddesktop: remote desktop system for iPad 
 *
 * Copyright (C) 2010 Kazuhiro Satoh
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, see <http://www.gnu.org/licenses/>.
 */

#import "ShortcutView.h"


enum ShortcutViewPaneId{
	PANE_COMMAND = 0,
	PANE_METAKEY,
	
	NUM_PANES
};


struct ButtonTemplate{
	char* title;
	BOOL isToggle;
	float col;
	float row;
	float colSize;
	float rowSize;
	BOOL useSmallFont;
	int keyCode;        //  (>=1: keyCode, <1: -paneId)
};


const static struct ButtonTemplate paneSelectButtons[] = {
	{"Key",      true, 0, 0, 3, 1.2, true, -PANE_METAKEY},
	{"Shortcut", true, 3, 0, 3, 1.2, true, -PANE_COMMAND},
	{"",        false, 0, 0, 2, 2, false, 0},  // terminater
};


const static struct ButtonTemplate commandButtons[] = {
	{"Maximize Window", false, 0,  0, 4, 2, true, DD_KEY_MAXIMIZE_WINDOW},
	{"Minimize Window", false, 4,  0, 4, 2, true, DD_KEY_MINIMIZE_WINDOW},
	{"Close Window",    false, 8,  0, 4, 2, true, DD_KEY_CLOSE_WINDOW},
	{"CTRL+ALT+DEL",    false, 12, 0, 4, 2, true, DD_KEY_CTRL_ALT_DEL},
	{"Undo (Ctrl+Z)",   false, 0,  2, 4, 2, true, DD_KEY_UNDO},
	{"Cut (Ctrl+X)",    false, 4,  2, 4, 2, true, DD_KEY_CUT},
	{"Copy (Ctrl+C)",   false, 8,  2, 4, 2, true, DD_KEY_COPY},
	{"Paste (Ctrl+V)",  false, 12, 2, 4, 2, true, DD_KEY_PASTE},
	{"",                false, 0,  0, 2, 2, false, 0},  // terminater
};

const static struct ButtonTemplate metakeyButtons[] = {
	{"Ctrl",  true,  0, 0, 2, 4, false, DD_KEY_CONTROL},
	{"Esc",  false,  2, 0, 2, 2, false, DD_KEY_ESCAPE},
	{"Alt",   true,  2, 2, 2, 2, false, DD_KEY_ALT},
	{"Tab",  false,  4, 0, 2, 2, false, DD_KEY_TAB},
	{"Shift", true,  4, 2, 2, 2, false, DD_KEY_SHIFT},
	{"Ins",  false,  6, 0, 2, 2, false, DD_KEY_INSERT},
	{"Del",  false,  6, 2, 2, 2, false, DD_KEY_DELETE},
	{"Home", false,  8, 0, 2, 2, false, DD_KEY_HOME},
	{"End",  false,  8, 2, 2, 2, false, DD_KEY_END},
	{"PgUp", false, 10, 0, 2, 2, false, DD_KEY_PAGEUP},
	{"PgDn", false, 10, 2, 2, 2, false, DD_KEY_PAGEDOWN},
	{"BS",   false, 12, 0, 2, 2, false, DD_KEY_BACKSPACE},
	{"Enter",false, 14, 0, 3, 2, false, DD_KEY_RETURN},
	{"Space",false, 12, 2, 5, 2, false, DD_KEY_SPACE},
	{"←",    false, 17, 2, 2, 2, false, DD_KEY_LEFT},
	{"↑",    false, 19, 0, 2, 2, false, DD_KEY_UP},
	{"↓",    false, 19, 2, 2, 2, false, DD_KEY_DOWN},
	{"→",    false, 21, 2, 2, 2, false, DD_KEY_RIGHT},
	{"",     false, 0, 0, 2, 2, false, 0},  // terminater
};

const static struct ButtonTemplate metakeyJPButtons[] = {
	{"半/全", false, 0, 0, 2, 2, false, DD_KEY_HANKAKU_ZENKAKU},
	{"Ctrl",  true,  0, 2, 2, 2, false, DD_KEY_CONTROL},
	{"Esc",  false,  2, 0, 2, 2, false, DD_KEY_ESCAPE},
	{"Alt",   true,  2, 2, 2, 2, false, DD_KEY_ALT},
	{"Tab",  false,  4, 0, 2, 2, false, DD_KEY_TAB},
	{"Shift", true,  4, 2, 2, 2, false, DD_KEY_SHIFT},
	{"Ins",  false,  6, 0, 2, 2, false, DD_KEY_INSERT},
	{"Del",  false,  6, 2, 2, 2, false, DD_KEY_DELETE},
	{"Home", false,  8, 0, 2, 2, false, DD_KEY_HOME},
	{"End",  false,  8, 2, 2, 2, false, DD_KEY_END},
	{"PgUp", false, 10, 0, 2, 2, false, DD_KEY_PAGEUP},
	{"PgDn", false, 10, 2, 2, 2, false, DD_KEY_PAGEDOWN},
	{"BS",   false, 12, 0, 2, 2, false, DD_KEY_BACKSPACE},
	{"Enter",false, 14, 0, 3, 2, false, DD_KEY_RETURN},
	{"Space",false, 12, 2, 5, 2, false, DD_KEY_SPACE},
	{"←",    false, 17, 2, 2, 2, false, DD_KEY_LEFT},
	{"↑",    false, 19, 0, 2, 2, false, DD_KEY_UP},
	{"↓",    false, 19, 2, 2, 2, false, DD_KEY_DOWN},
	{"→",    false, 21, 2, 2, 2, false, DD_KEY_RIGHT},
	{"",     false, 0, 0, 2, 2, false, 0},  // terminater
};


static const int BUTTON_WIDTH = 28;
static const int BUTTON_HEIGHT = 20;
static const int BUTTON_MARGIN = 2;




@interface ShortcutView ()
- (UIView*)addPaneWithButtonArray:(struct ButtonTemplate*) buttonArray;
- (void)changePaneSelection:(enum ShortcutViewPaneId)paneId;
@end


@implementation ShortcutView

@synthesize delegate = _delegate;


- (id)init{
	if((self = [super init])){
		_smallFont = nil;
		struct ButtonTemplate* metakeyButtonsForCurrentLocale;
		NSString* locale = [[NSLocale autoupdatingCurrentLocale] localeIdentifier];
		
		//NSLog(@"locale: %@", locale);
		
		if([locale isEqualToString:@"ja_JP"]){
			metakeyButtonsForCurrentLocale = (struct ButtonTemplate*)metakeyJPButtons;
		}
		else{
			metakeyButtonsForCurrentLocale = (struct ButtonTemplate*)metakeyButtons;
		}
		
		UIView* commandPane = [self addPaneWithButtonArray:(struct ButtonTemplate*)commandButtons];
		UIView* metakeyPane = [self addPaneWithButtonArray:(struct ButtonTemplate*)metakeyButtonsForCurrentLocale];
		_paneSelectionView  = [self addPaneWithButtonArray:(struct ButtonTemplate*)paneSelectButtons];
		
		_paneArray = [[NSArray alloc] initWithObjects: commandPane, metakeyPane, nil];
		
		
		{ // set frame size
			int width = 0;
			int height = 0;
			for(int i = 0; i < [_paneArray count]; i++){
				UIView* pane = (UIView*)[_paneArray objectAtIndex:i];
				pane.frame = CGRectMake(0,
										_paneSelectionView.frame.size.height + BUTTON_HEIGHT / 2,
										pane.frame.size.width,
										pane.frame.size.height);
				width  = MAX(width,  pane.frame.origin.x + pane.frame.size.width);
				height = MAX(height, pane.frame.origin.y + pane.frame.size.height);
			}
			
			self.frame = CGRectMake(0, 0, width, height);
		}
		
		_paneSelectionView.hidden = false;
		[self changePaneSelection:PANE_METAKEY];
	}		
	return self;
}


- (void)dealloc{
	[_paneArray release];
	[super dealloc];
}


- (UIView*)addPaneWithButtonArray:(struct ButtonTemplate*) buttonArray
{
	int pane_width = 0;
	int pane_height = 0;
	
	UIView* pane = [[UIView alloc] init];

	for(int i = 0; buttonArray[i].title != ""; i++){
		UIButton *button = [[UIButton buttonWithType: UIButtonTypeRoundedRect] retain];
		[button setTitle:[NSString stringWithCString:buttonArray[i].title encoding:NSUTF8StringEncoding]
				forState:UIControlStateNormal];
		if (buttonArray[i].useSmallFont) {
			if(_smallFont == nil){
				_smallFont = [button.titleLabel.font fontWithSize:button.titleLabel.font.pointSize*3/4];
			}
			button.titleLabel.font = _smallFont;
		}
		button.frame = CGRectMake(buttonArray[i].col * (BUTTON_WIDTH  + BUTTON_MARGIN),
								  buttonArray[i].row * (BUTTON_HEIGHT + BUTTON_MARGIN),
								  buttonArray[i].colSize * (BUTTON_WIDTH  + BUTTON_MARGIN) - BUTTON_MARGIN,
								  buttonArray[i].rowSize * (BUTTON_HEIGHT  + BUTTON_MARGIN) - BUTTON_MARGIN);
		button.alpha = 0.7;
		[button setTitleColor:UIColor.blueColor forState:UIControlStateSelected];
		button.tag = (int)&(buttonArray[i]);
		[button addTarget:self action:@selector(respondToButtonDown:) forControlEvents:UIControlEventTouchDown];
		[button addTarget:self action:@selector(respondToButtonUp:) forControlEvents:UIControlEventTouchUpInside];
		[button addTarget:self action:@selector(respondToButtonUp:) forControlEvents:UIControlEventTouchUpOutside];

		pane_width = MAX(pane_width, button.frame.origin.x + button.frame.size.width);
		pane_height = MAX(pane_height, button.frame.origin.y + button.frame.size.height);
		[pane addSubview:button];
	}
	
	pane.frame = CGRectMake(0, 0, pane_width, pane_height);
	[self addSubview: pane];
	[pane release];
	
	return pane;
}


- (void)changePaneSelection:(enum ShortcutViewPaneId)paneId
{
	int i;

	for(i = 0; i < [_paneArray count]; i++){
		UIView* pane = (UIView*)[_paneArray objectAtIndex:i];
		pane.hidden = true;
	}
	((UIView*)[_paneArray objectAtIndex:paneId]).hidden = false;
	
	for(i = 0; i < [_paneSelectionView.subviews count]; i++){
		UIButton* button = (UIButton*)[_paneSelectionView.subviews objectAtIndex:i];
		if(((struct ButtonTemplate*)button.tag)->keyCode == -paneId){
			button.selected = true;
		}
		else{
			button.selected = false;
		}
	}
}


- (IBAction)respondToButtonDown:(UIButton*)sender {
	struct ButtonTemplate* btn = (struct ButtonTemplate*)sender.tag;

	if(btn->keyCode > 0){
		if (btn->isToggle) {
			if(!sender.selected){
				[_delegate shortcutKeyWasDown:btn->keyCode];
				sender.selected = true;
			}
			else{
				[_delegate shortcutKeyWasUp:btn->keyCode];
				sender.selected = false;
			}
		}
		else{
			[_delegate shortcutKeyWasDown:btn->keyCode];
		}
	}
}


- (IBAction)respondToButtonUp:(UIButton*)sender {
	struct ButtonTemplate* btn = (struct ButtonTemplate*)sender.tag;
	
	if(btn->keyCode > 0){
		if (!btn->isToggle) {
			[_delegate shortcutKeyWasUp:btn->keyCode];
		}
	}
	else {
		[self changePaneSelection:(enum ShortcutViewPaneId)-btn->keyCode];
	}
}



@end
